package utils

import (
	"log"

	"github.com/go-git/go-git/v5"
)

func UpdateGitRepo(path string) error {
	log.Println("git init local update repo: ", path)
	r, err := git.PlainOpen(path)
	if err != nil {
		return err
	}

	w, err := r.Worktree()
	if err != nil {
		return err
	}

	log.Println("git local pull origin")
	err = w.Pull(&git.PullOptions{RemoteName: "origin"})
	if err != nil {
		return err
	}

	ref, err := r.Head()
	if err != nil {
		return err
	}

	commit, err := r.CommitObject(ref.Hash())
	if err != nil {
		return err
	}

	commit = commit
	// log.Println(commit) //debug
	log.Println("git pull: done")
	return nil
}
